package com.astersoft.lib.text;

/*
 * Copyright 2012 AsterSoft personal developer - Licensed under the Apache
 * License, Version 2.0 (the "License"); you may not use this file except in
 * compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law
 * or agreed to in writing, software distributed under the License is
 * distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied. See the License for the specific language
 * governing permissions and limitations under the License.
 */

import java.io.IOException;
import java.io.Reader;

import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.parser.ParserDelegator;

/**
 * Cleanup HTML text from the HTML tag
 * 
 * How to use:
 * 
 * URL url = new URL("http://www.sintesinet.com");
 * BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
 * DeTag d = new DeTag(in);
 * d.parse();
 * in.close();
 * System.out.println(d.toString());
 * 
 * @author dario.durzo
 * @version 0.1
 * @created.date 2009.08.26
 */
public class ASDeTag extends HTMLEditorKit.ParserCallback {
	StringBuffer txt;
	Reader reader;

	// empty default constructor
	public ASDeTag() {}

	// more convienient constructor
	public ASDeTag(Reader r) {
		setReader(r);
	}

	public void setReader(Reader r) { reader = r; }

	public void parse() throws IOException {
		txt = new StringBuffer();
		ParserDelegator parserDelegator = new ParserDelegator();
		parserDelegator.parse(reader, this, true);
	}

	public void handleText(char[] text, int pos) {
		txt.append(text);
	}

	public String toString() {
		return txt.toString();
	}
}